package com.reactim.message.config.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 连接超时时间验证器
 */
public class ConnectionTimeoutValidator implements ConstraintValidator<ValidConnectionTimeout, Integer> {
    
    private int min;
    private int max;
    
    @Override
    public void initialize(ValidConnectionTimeout constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }
    
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return true; // 让@NotNull处理null值
        }
        
        if (value < min || value > max) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(
                String.format("连接超时时间必须在%d分钟到%d分钟之间，当前值: %d分钟", min, max, value)
            ).addConstraintViolation();
            return false;
        }
        
        return true;
    }
}